{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loop Break and Continue inside a Try...Except...Finally"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Recall that in a ``try`` statement, the ``finally`` clause always runs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this always executes\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 1\n",
    "try:\n",
    "    a / b\n",
    "except ZeroDivisionError:\n",
    "    print('division by 0')\n",
    "finally:\n",
    "    print('this always executes')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "division by 0\n",
      "this always executes\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 0\n",
    "try:\n",
    "    a / b\n",
    "except ZeroDivisionError:\n",
    "    print('division by 0')\n",
    "finally:\n",
    "    print('this always executes')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, what happens when using a ``try`` statement within a ``while`` loop, and a ``continue`` or ``break`` statement is encountered?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "1, 1 - always executes\n",
      "1, 1 - main loop\n",
      "-------------\n",
      "2, 0 - division by 0\n",
      "2, 0 - always executes\n",
      "-------------\n",
      "3, -1 - always executes\n",
      "3, -1 - main loop\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "b = 2\n",
    "\n",
    "while a < 3:\n",
    "    print('-------------')\n",
    "    a += 1\n",
    "    b -= 1\n",
    "    try:\n",
    "        res = a / b\n",
    "    except ZeroDivisionError:\n",
    "        print('{0}, {1} - division by 0'.format(a, b))\n",
    "        res = 0\n",
    "        continue\n",
    "    finally:\n",
    "        print('{0}, {1} - always executes'.format(a, b))\n",
    "        \n",
    "    print('{0}, {1} - main loop'.format(a, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see in the above result, the ``finally`` code still executed, even though the current iteration was cut short with the ``continue`` statement. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This works the same with a ``break`` statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "1, 1 - always executes\n",
      "1, 1 - main loop\n",
      "-------------\n",
      "2, 0 - division by 0\n",
      "2, 0 - always executes\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "b = 2\n",
    "\n",
    "while a < 3:\n",
    "    print('-------------')\n",
    "    a += 1\n",
    "    b -= 1\n",
    "    try:\n",
    "        res = a / b\n",
    "    except ZeroDivisionError:\n",
    "        print('{0}, {1} - division by 0'.format(a, b))\n",
    "        res = 0\n",
    "        break\n",
    "    finally:\n",
    "        print('{0}, {1} - always executes'.format(a, b))\n",
    "        \n",
    "    print('{0}, {1} - main loop'.format(a, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can even combine all this with the ``else`` clause:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "1, 1 - always executes\n",
      "1, 1 - main loop\n",
      "-------------\n",
      "2, 0 - division by 0\n",
      "2, 0 - always executes\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "b = 2\n",
    "\n",
    "while a < 3:\n",
    "    print('-------------')\n",
    "    a += 1\n",
    "    b -= 1\n",
    "    try:\n",
    "        res = a / b\n",
    "    except ZeroDivisionError:\n",
    "        print('{0}, {1} - division by 0'.format(a, b))\n",
    "        res = 0\n",
    "        break\n",
    "    finally:\n",
    "        print('{0}, {1} - always executes'.format(a, b))\n",
    "        \n",
    "    print('{0}, {1} - main loop'.format(a, b))\n",
    "else:\n",
    "    print('\\n\\nno errors were encountered!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "1, 4 - always executes\n",
      "1, 4 - main loop\n",
      "-------------\n",
      "2, 3 - always executes\n",
      "2, 3 - main loop\n",
      "-------------\n",
      "3, 2 - always executes\n",
      "3, 2 - main loop\n",
      "\n",
      "\n",
      "no errors were encountered!\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "b = 5\n",
    "\n",
    "while a < 3:\n",
    "    print('-------------')\n",
    "    a += 1\n",
    "    b -= 1\n",
    "    try:\n",
    "        res = a / b\n",
    "    except ZeroDivisionError:\n",
    "        print('{0}, {1} - division by 0'.format(a, b))\n",
    "        res = 0\n",
    "        break\n",
    "    finally:\n",
    "        print('{0}, {1} - always executes'.format(a, b))\n",
    "        \n",
    "    print('{0}, {1} - main loop'.format(a, b))\n",
    "else:\n",
    "    print('\\n\\nno errors were encountered!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
